package MsgBus

import "sync"

type container struct {
    store map[int64]*Subscriber
    mu    sync.RWMutex
}

func newContianer() *container {
    x := &container{}
    x.store = make(map[int64]*Subscriber)
    return x
}

func (s *container) add(p *Subscriber) *Subscriber {
    s.mu.Lock()
    defer s.mu.Unlock()
    p_old, ok := s.store[p.ID()]
    if ok {
        return p_old
    }
    s.store[p.ID()] = p
    return p
}

func (s *container) remove(p *Subscriber) int {
    s.mu.Lock()
    defer s.mu.Unlock()
    delete(s.store, p.ID())
    return len(s.store)
}

func (s *container) iterate(f func(pipe *Subscriber)) {
    s.mu.RLock()
    defer s.mu.RUnlock()

    for _, v := range s.store {
        f(v)
    }
}
